package org.zfes.snowier.cloud.zbss.cms.column.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.column.service.ICmsColumnGroupContributeService;
import org.zfes.snowier.cloud.zbss.cms.column.dao.CmsColumnGroupContributeMapper;
import org.zfes.snowier.cloud.zbss.cms.column.model.CmsColumnGroupContribute;

import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class CmsColumnGroupContributeServiceImpl implements ICmsColumnGroupContributeService{
 	@Autowired
	private CmsColumnGroupContributeMapper cmsColumnGroupContributeMapper;
	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsColumnGroupContribute(CmsColumnGroupContribute cmsColumnGroupContribute) {
		ZBeanUtil.validateBean(cmsColumnGroupContribute);
		cmsColumnGroupContribute.setStatus(Byte.valueOf("1"));
		cmsColumnGroupContribute.setCreateTime(ZDateUtil.newDate());
		cmsColumnGroupContribute.setUpdateTime(ZDateUtil.newDate());
		cmsColumnGroupContributeMapper.insertSelective(cmsColumnGroupContribute);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsColumnGroupContribute> loadCmsColumnGroupContributeById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsColumnGroupContributeMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsColumnGroupContribute(CmsColumnGroupContribute cmsColumnGroupContribute) {
		ZBeanUtil.validateBean(cmsColumnGroupContribute);
		cmsColumnGroupContribute.setUpdateTime(ZDateUtil.newDate());
		cmsColumnGroupContributeMapper.updateByPrimaryKeySelective(cmsColumnGroupContribute);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsColumnGroupContribute(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		cmsColumnGroupContributeMapper.deleteByPrimaryKey(id);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsColumnGroupContributeStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsColumnGroupContributeMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsColumnGroupContributeDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(cmsColumnGroupContributeMapper.selectListCount(pm), cmsColumnGroupContributeMapper.selectMapListPage(pm));
		return ds;
	}
	
	//批量插入
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void createCmsColumnGroupContribute(Long columnId, String contributeGroupIds) {
		
		ZAssert.notNull(columnId, "栏目id不能为空");
		ZAssert.notNull(contributeGroupIds, "groupIds不能为空");
		ZAssert.hasText(contributeGroupIds, "groupIds不能为空");
		
		List<Long> contributeGroupIdList=Arrays.asList(contributeGroupIds.split(",")).stream().map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
		CmsColumnGroupContribute groupContribute = new CmsColumnGroupContribute();
		ZBeanUtil.setCreateInfo(groupContribute);
		groupContribute.setColumnId(columnId);
		
		//插入
		for(int i=0;i<contributeGroupIdList.size();i++){
			groupContribute.setGroupId(contributeGroupIdList.get(i));
			cmsColumnGroupContributeMapper.insertSelective(groupContribute);
		}
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsColumnGroupContributeByColumnId(Long columnId) {
		ZAssert.notNull(columnId, "栏目id不能为空");
		cmsColumnGroupContributeMapper.deleteByColumnId(columnId);
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsColumnGroupContributeStatusByColumnId(Long columnId, Byte status) {
		ZAssert.notNull(columnId, "栏目id不能为空");
		cmsColumnGroupContributeMapper.updateStatusByColumnId(columnId, status);
		
	}
	
}